<template>
  <el-dialog 
    title="选择配置模板" 
    :visible.sync="dialogVisible"
    width="600px"
    append-to-body
  >
    <div class="template-list">
      <el-card 
        v-for="(template, key) in templates" 
        :key="key"
        class="template-card"
        shadow="hover"
      >
        <div slot="header" class="template-header">
          <h4>{{ template.projectName }}</h4>
          <el-tag type="success">{{ template.standardCode }}</el-tag>
        </div>
        <div class="template-content">
          <p class="template-desc">{{ template.description }}</p>
          <div class="template-stats">
            <span>参数组: {{ getTemplateGroupCount(template) }}</span>
            <span>参数: {{ getTemplateParamCount(template) }}</span>
          </div>
        </div>
        <div class="template-actions">
          <el-button type="primary" size="mini" @click="$emit('apply', key)">应用模板</el-button>
        </div>
      </el-card>
    </div>
    <div slot="footer">
      <el-button @click="dialogVisible = false">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'TemplateDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    templates: {
      type: Object,
      required: true
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.visible;
      },
      set(value) {
        this.$emit('update:visible', value);
      }
    }
  },
  methods: {
    getTemplateGroupCount(template) {
      return template.paramGroups ? template.paramGroups.length : 0;
    },
    getTemplateParamCount(template) {
      let count = 0;
      if (template.paramGroups) {
        template.paramGroups.forEach(group => {
          if (group.isNested && group.childGroups) {
            group.childGroups.forEach(childGroup => {
              count += childGroup.paramItems ? childGroup.paramItems.length : 0;
            });
          } else {
            count += group.paramItems ? group.paramItems.length : 0;
          }
        });
      }
      return count;
    }
  }
};
</script>

<style scoped>
.template-list {
  max-height: 500px;
  overflow-y: auto;
}
.template-card {
  margin-bottom: 15px;
}
.template-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
}
.template-header h4 {
  margin: 0;
  font-size: 16px;
}
.template-content {
  margin-bottom: 15px;
}
.template-desc {
  color: #606266;
  font-size: 14px;
  margin: 0 0 10px 0;
}
.template-stats {
  font-size: 12px;
  color: #909399;
}
.template-stats span {
  margin-right: 15px;
}
.template-actions {
  text-align: right;
}
</style>